package com.xiaomei.manager.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.UUID;


import com.xiaomei.manager.dao.TExperienceRec2Mapper;
import com.xiaomei.manager.model.TExperienceRec2;
import com.xiaomei.manager.model.TExperienceRec2Key;
import com.xiaomei.manager.web.dto.ActivityStateAndUserDto;
import com.xiaomei.manager.web.dto.req.ActivityRecQueryDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.xiaomei.manager.dao.ExperienceMapper;
import com.xiaomei.manager.dao.ExperienceRecordMapper;
import com.xiaomei.manager.model.Experience;
import com.xiaomei.manager.model.ExperienceRecord;
import com.xiaomei.manager.service.IExperienceService;
import com.xiaomei.manager.web.dto.req.ExperienceQueryDto;
import com.xiaomei.manager.web.page.Page;

@Service
public class ExperienceServiceImpl implements IExperienceService{
	@Autowired
	private ExperienceMapper experiMapper;
	@Autowired
	private ExperienceRecordMapper experiReMapper;
	@Autowired
	private TExperienceRec2Mapper tExperienceRec2Mapper;

	@Override
	public List<Experience> selectAll() {
		// TODO Auto-generated method stub
		return experiMapper.selectAll();
	}
	@Override
	public List<Experience> selectByParam(ExperienceQueryDto dto,
			Page<Experience> page) {
		page.setParamDto(dto);	
		return experiMapper.selectByParam(page);
	}
	@Override
	public List<ActivityStateAndUserDto> selectSignupUserByParam(ActivityRecQueryDto dto, Page<ActivityStateAndUserDto> page) {
		page.setParamDto(dto);
		return experiMapper.selectSignupUserByParam(page);
	}
	/**
	 * 将认证过程中的体验券分配给用户
	 */
	@Override
	public boolean distributeTicket(String userCode, Integer id) {
		Experience exp = experiMapper.selectByPrimaryKey(id);
		if(exp.getTicketAlreadyCount() == null){
			exp.setTicketAlreadyCount(0);
		}
		exp.setTicketAlreadyCount(exp.getTicketAlreadyCount() + 1);
		Integer i = exp.getTicketCount() - exp.getTicketAlreadyCount();
		if(i <= 0){
			return false;
		}
		ExperienceRecord expR = new ExperienceRecord();
		expR.setExperienceId(id);
		expR.setInputTime(new Timestamp(System.currentTimeMillis()));
		expR.setOrderCode("orderCode");
		expR.setState("0");
		expR.setUserCode(userCode);
		if(experiReMapper.insert(expR) > 0 && experiMapper.updateByPrimaryKeySelective(exp) > 0){
			return true;
		}else{
			throw new RuntimeException();
		}
		
	}
	
	@Override
	public List<Experience> selectByTicket(String ticketType) {
		// TODO Auto-generated method stub
		return experiMapper.selectByTicketType(ticketType);
	}
	
	@Override
	public Experience selectExperienceById(Integer id) {
		// TODO Auto-generated method stub
		return experiMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public boolean createExperience(Experience experience) {
		// TODO Auto-generated method stub
		if(experiMapper.insert(experience) > 0){
			if(experience.getActivityUrl()==null || experience.getActivityUrl().isEmpty()) {
				experience.setActivityUrl("http://xm.xuhuangzi.com:8080/xiaomei/experience/"+experience.getId());
				experiMapper.updateByPrimaryKey(experience);
			}
			return true;
		}
		return false;
	}
	
	@Override
	public Integer selectMaxID() {
		return experiMapper.selectMaxId();
	}
	
	@Override
	public Experience findOne(Integer id) {
		// TODO Auto-generated method stub
		return experiMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public boolean updateExperience(Experience experience) {
		// TODO Auto-generated method stub
		if(experience.getActivityUrl()==null || experience.getActivityUrl().isEmpty()) {
			experience.setActivityUrl("http://xm.xuhuangzi.com:8080/xiaomei/experience/"+experience.getId());
			experiMapper.updateByPrimaryKey(experience);
		}
		if(experiMapper.updateByPrimaryKeySelective(experience) > 0){
			return true;
		}
		return false;
	}
	
	@Override
	public boolean deleteExperience(Integer id) {
		if(experiMapper.deleteByPrimaryKey(id) > 0){
			return true;
		}
		return false;
	}

	@Override
	public boolean doActiveUser(String userCode, int id, int state) {
		//if(state != 2 || state != 3) return false;
		TExperienceRec2Key key = new TExperienceRec2Key();
		key.setUserCode(userCode);
		key.setExperienceId(id);

		TExperienceRec2 rec = tExperienceRec2Mapper.selectByPrimaryKey(key);
		if (rec == null) {
			return false;
		}
		if(state == 2 && rec.getState()==1) {
			rec.setUpdateTime(new Date());
			rec.setState(2);
			UUID uuid  =  UUID.randomUUID();
			rec.setValidateCode(uuid.toString().substring(0,8).toUpperCase());
			return tExperienceRec2Mapper.updateByPrimaryKeySelective(rec) > 0;
		}
		if(state == 3 && rec.getState()==2) {
			rec.setUpdateTime(new Date());
			rec.setState(3);
			return tExperienceRec2Mapper.updateByPrimaryKeySelective(rec) > 0;
		}
		return false;
	}
}
